%**************************************************************
% Reads the data from ultrasound (.bin) and wire-mesh (.v) files
% Autor: Carlos Amaral (ceamaral@hotmail.com)
% data: 06/10/2010
% functions that are called: read_US, read_WM
% sensor_type can be 'US' or 'WM'
% num_channels can be '1_channels' or '2_channels'
%**************************************************************

function read_all_measurements(sensor_type,num_channels,freq_aq,tube_diameter,files_list)
% If sensor_type is 'US', analysis of ultrasound
% If sensor_type is 'WM', analysis of wire-mesh
% If num_channels is '1_channels' process only sensor 1
% If num_channels is '2_channels' process only sensor 2

close all %Closes all Figures already open
if nargin==4        
    if (strcmp(sensor_type,'WM'))   %looks for files types   
       files_list = dir('*-8_1.v');                %cria matriz com nomes dos arquivos . (WM 1)
       num_files = length(files_list);             % Check the number of files from WM sensor 1
        if (num_files == 0)                        %Tests if files_list are in the root of files_list directory
            files_list = dir('.\VFiles\*-8_1.v');                %cria matriz com nomes dos arquivos (Wiremesh 1)          
        end
    end

    if (strcmp(sensor_type,'WM')) && (strcmp(num_channels,'2_channels'))   %looks for files types   
       files_list_WM2 = dir('*-8_2.v');                %cria matriz com nomes dos arquivos . (WM 1)
       num_files_WM2 = length(files_list_WM2);         % Check the number of files from WM sensor 2
        if (num_files_WM2 == 0)                        %Tests if files_list are in the root of files_list directory
            files_list_WM2 = dir('.\VFiles\*-8_2.v');                %cria matriz com nomes dos arquivos (Wiremesh 1)          
            num_files_WM2 = length(files_list_WM2); % Check the number of files from WM sensor 2
        end    
    end

    if (strcmp(sensor_type,'US'))   %looks for files types   
        files_list = dir('*.bin');                %cria matriz com nomes dos arquivos . (ultrasound 1) 
    end
    num_files = length(files_list);
elseif nargin==5
    %files comes from the function
    num_files = length(files_list);
    num_files_WM2=num_files;
    
end

    
    if(num_files > 0)          %testa se existem arquivos .
        fprintf('\n Reading %s sensor (%s).\n',sensor_type,num_channels);
        nr=0; 
        for i=1:num_files                                  %Creates a matrix of JL and JG using the name of the files_list                             
            if (strcmp(sensor_type,'US'))  %looks for files types  
                fprintf('     Reading %s data\n', files_list(i).name);
                name_file = strrep(files_list(i).name,'.bin','');
                
                name=[name_file '.bin'];
               [void_US1,waterlevel_US1] = read_US(name,tube_diameter,freq_aq,'US1');   %Calculates alpha from .BIN ultrasound files
               if (strcmp(num_channels,'2_channels'))   %looks for files types                       
                    [void_US2,waterlevel_US2] = read_US(name,tube_diameter,freq_aq,'US2');   %Calculates alpha from .BIN ultrasound files
               end
            end

            if (strcmp(sensor_type,'WM')) %looks for files types   
                name_file = strrep(files_list(i).name,'-8_1.v','');
                fprintf('     Reading %s data\n', files_list(i).name);
                name=[name_file '-8_1.v'];
                [void_WM1,midle_electrode_void_WM1,waterlevel_WM1] = read_WM(name,tube_diameter);  %returns the mean void fraction and the midle electrode measurements            
                if (strcmp(num_channels,'2_channels')) && (num_files_WM2 > 0)   %looks for files types                                       
                    name=[name_file '-8_2.v'];
                    fprintf('     Reading %s data\n', name);
                    [void_WM2,midle_electrode_void_WM2,waterlevel_WM2] = read_WM(name,tube_diameter);                                   
                end        
            end            

            if (exist('.\Matlab_data','dir') == 0) %Creates Result directory if it does not exist yeat
              mkdir('Matlab_data');  
            end          

            file_path_output = ['.\Matlab_data\' name_file '_' sensor_type '.mat']; %Saves workspace in \Matlab_data directory
            if (exist(file_path_output,'file') == 0) %Erases data before
              delete(file_path_output);  
            end
            save(file_path_output);          
        end

    end

return